package com.androidhive.androidlistview;

import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class FEditProfile extends Activity {
	DatabaseHandler db=new DatabaseHandler(this);
	AlertDialogManager alert = new AlertDialogManager();
	SessionManager session;
	EditText name,cnu,eid,floc;
	 String fCNU=new String();
	 String fEID=new String();
	 String fFLOC=new String();
	 String fFSTATE=new String();
Spinner state;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fedit_profile);
		// Show the Up button in the action bar.
		//getActionBar().setDisplayHomeAsUpEnabled(true);
		state = (Spinner) findViewById(R.id.spinner1);
		 name = (EditText)findViewById(R.id.editText1);
	 cnu = (EditText)findViewById(R.id.editText2);
		 eid = (EditText)findViewById(R.id.editText3);
		 floc = (EditText)findViewById(R.id.editText4);
		Spinner fstate = (Spinner)findViewById(R.id.spinner1);
		 Intent intent = getIntent();
		    
		     fCNU = intent.getStringExtra(FViewprofile.NAME);
		   Log.d("Edit:",fCNU);
		     name.setHint(fCNU.split("#")[0]);
		     cnu.setHint(fCNU.split("#")[1]);
		     eid.setHint(fCNU.split("#")[2]);
		     floc.setHint(fCNU.split("#")[3]);
		     
		  //  session.logoutUser();
		
		 //   HashMap<String, String> user = session.getUserDetails();
	      
	    //    String VendorId = (user.get(SessionManager.Vendor_Id)); 
		     
		     
		    Button submit = (Button) findViewById(R.id.button1);
		        submit.setOnClickListener(new View.OnClickListener() {
					
					//@Override
					public void onClick(View arg0) {
						 String fNAME=new String();	
					fNAME = name.getText().toString().trim();	
					fCNU=cnu.getText().toString().trim();	
					fEID=eid.getText().toString().trim();	
					fFLOC=floc.getText().toString().trim();	
					fFSTATE=String.valueOf(state.getSelectedItem());
					session = new SessionManager(getApplicationContext());
				    HashMap<String, String> user = session.getUserDetails();
				    String farmerId = (user.get(SessionManager.Vendor_Id)); 
					
					Log.d("new",farmerId+fNAME+fCNU+fEID+fFLOC+fFSTATE);
					if(fNAME.trim().length()<=0 || fCNU.trim().length()<=0 || fEID.trim().length()<=0 || fFLOC.trim().length()<=0 ){
						
						alert.showAlertDialog(FEditProfile.this, "Edit profile failed..", "Please enter all the entries.", false);
				
				}	
					else{
						db.updateFarmerProfile(Integer.parseInt(farmerId), fNAME, fCNU, fEID, fFLOC, fFSTATE);
						
						AlertDialog.Builder alt_bld = new AlertDialog.Builder(FEditProfile.this);
					    alt_bld.setMessage("Do you want to close this application ?")
					    .setCancelable(false)
					    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog, int id) {
					    	Intent i = new Intent(getApplicationContext(), FarmerMenu.class);
							startActivity(i);
								finish();
					    	
								
							 
							   
					    	
					    }
					    });
					   
					    AlertDialog alert = alt_bld.create();
					    // Title for AlertDialog
					 //   alert.setTitle("Terms And Conditions");
					    alert.setMessage("Your Profile is updated successfully. Thank you.");
					    // Icon for AlertDialog
					    alert.setIcon(R.drawable.success);
					    alert.show();
						
				    	
						
					}
						
						
						
						
					}});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_fedit_profile, menu);
		return true;
	}

	
	

}
